package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.RescueRecord;
import com.gc.business.service.IRescueRecordService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 救援记录 控制层
 * 
 * @author liujinteng
 * @date 2020-11-21
 */
@Slf4j
@Controller
@RequestMapping("/business/rescueRecord")
public class RescueRecordController extends BaseController {
    private String prefix = "business/rescueRecord";
	
	private final IRescueRecordService rescueRecordService;

	@Autowired
	public RescueRecordController(IRescueRecordService rescueRecordService) {
		this.rescueRecordService = rescueRecordService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:rescueRecord:view")
	@GetMapping
	public String rescueRecord(){
	    return prefix + "/rescueRecord";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{recordId}")
	public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap){
		RescueRecord rescueRecord = rescueRecordService.getById(recordId);
		mmap.put("rescueRecord", rescueRecord);
		return prefix + "/edit";
	}
	
	/**
	 * 查询救援记录列表
	 */
	@RequiresPermissions("business:rescueRecord:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(RescueRecord rescueRecord){
		startPage();
        List<RescueRecord> list = rescueRecordService.list(rescueRecord);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存救援记录
	 */
	@RequiresPermissions("business:rescueRecord:add")
	@Log(title = "救援记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(RescueRecord rescueRecord){
		rescueRecordService.save(rescueRecord);
		return Result.success();
	}

	/**
	 * 修改保存救援记录
	 */
	@RequiresPermissions("business:rescueRecord:edit")
	@Log(title = "救援记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(RescueRecord rescueRecord){
		rescueRecordService.updateById(rescueRecord);
		return Result.success();
	}
	
	/**
	 * 删除救援记录
	 */
	@RequiresPermissions("business:rescueRecord:remove")
	@Log(title = "救援记录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		rescueRecordService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出救援记录列表
	 */
	@RequiresPermissions("business:rescueRecord:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(RescueRecord rescueRecord){
		List<RescueRecord> list = rescueRecordService.list(rescueRecord);
		ExcelUtil<RescueRecord> util = new ExcelUtil<>(RescueRecord.class);
		util.exportExcel(list, "rescueRecord");
		return Result.success();
	}
}
